/*
 * Created on 2005-6-28
 * Last modified on 2007-05-13
 * Powered by YeQiangWei.com
 */
package com.yeqiangwei.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.log4j.Logger;

public class FormatDateTime {
	
	private static Logger logger = Logger.getLogger(FormatDateTime.class);
	
	public static final int SECOND = 13;
	
	public static final int DAY_OF_MONTH = 5;
	
	public static final int DAY_OF_YEAR = 6;
	
	public static final int DAY_OF_WEEK = 7;
	
	public static final int HOUR_OF_DAY = 11;
	
	public static final int WEEK_OF_YEAR = 3;
	
	public static final int MONTH = 2;
	
	public static final int YEAR = Calendar.YEAR;
	
    /**
     * 设置间隔数后返回时间
     * @param type 间隔数类型 s秒 d天 h小时
     * @param i 间隔数
     * @param fromat_str 格式化时间的方式 如yyyy-MM-dd HH:mm:ss 
     * @return 
     */
    public static String dateAdd(int type, int i, String fromat_str)
    {
        return dateAdd(FormatDateTime.formatDateTime(fromat_str), type, i, fromat_str);        
    }

	public static long now(){
		return TypeChange.stringToLong(formatDateTime("yyyyMMddHHmmss"));
	}
	
    public static long formatDateTimeToLong(String ymd){
        return TypeChange.stringToLong(formatDateTime(ymd));
    }
    
    public static int formatDateTimeToInt(String ymd){
        return TypeChange.stringToInt(formatDateTime(ymd));
    }
    public static void main(String args[]){
//    	long l = FormatDateTime.now();
//    	System.out.println(l);
//    	String s = FormatDateTime.dateAdd(FormatDateTime.SECOND,10,"yyyyMMddHHmmss");
//    	System.out.println(s); 1189156459750
    	//System.out.println(now());
//    	Timestamp time=new Timestamp(System.currentTimeMillis());
//    	java.text.SimpleDateFormat isNow = new java.text.SimpleDateFormat("yyyyMMdd");
//    	System.out.println(isNow.format(time));
    	//Date date=new Timestamp(System.currentTimeMillis());
    }
    /**
     * 日期类操作工具
     * @author YeQiangWei by 我容易么我
     * Powered by YeQiangWei.com
     */
    public static String formatDateTime(String ymd){
        //格式化当前时间
        java.text.SimpleDateFormat isNow = new java.text.SimpleDateFormat(ymd);
        return isNow.format(new java.util.Date());
    }
    
    public static String formatDateTime(String ymd, String datetime){
     
        java.text.SimpleDateFormat isNow = new java.text.SimpleDateFormat(ymd);
        String now = "";
        try{
        	now = isNow.format(datetime);
        }catch(Exception e){
            logger.error(e.toString());
        }
        return now;
    }
    
    public static String decodeTime(long time){
    	return decodeTime(String.valueOf(time));
    }
    
    public static String decodeTime(String time){
    	StringBuffer sb = new StringBuffer("");
    	String str = time;
    	if(str.length()>=14){
	    	sb.append(str.substring(0,4));
	    	sb.append("-");
	    	sb.append(str.substring(4,6));
	    	sb.append("-");
	    	sb.append(str.substring(6,8));
	    	sb.append(" ");
	    	sb.append(str.substring(8,10));
	    	sb.append(":");
	    	sb.append(str.substring(10,12));
	    	sb.append(":");
	    	sb.append(str.substring(12,14));
    	}
    	return sb.toString();
    }
    
    public static boolean dateCompare(long time){
    	if(time>0){
    		return dateCompare(decodeTime(time));
    	}else{
    		return false;
    	}
    }
    
    /**
     * 比较当前日期和指定日期 return boolean
     * 如果当前日期在指定日期之后返回true否则返回flase
     * @param str
     * @return 
     * 2006-1-15 5:26:44
     */
    public static boolean dateCompare(String str){
        boolean bea = false;
        SimpleDateFormat sdf_d = new SimpleDateFormat("yyyy-MM-dd");
        String isDate = sdf_d.format(new java.util.Date());
        java.util.Date date1;
        java.util.Date date0;
        try {
            date1 = sdf_d.parse(str);
            date0= sdf_d.parse(isDate);
            if(date0.after(date1)){ 
                bea = true;
            }
        } catch (ParseException e) {
        	logger.error(e.toString());
            bea = false;
        }
        return bea;
    }
    
    /**
     * 比较当前月份和指定月份
     * 如果当前月份在指定月份之后返回true否则返回flase
     * @param str
     * @return 
     * 2006-1-15 5:26:57 made in YeQiangWei
     */
    public static boolean monthCompare(String str){
        boolean bea = false;
        SimpleDateFormat sdf_m = new SimpleDateFormat("yyyy-MM");
        String isMonth = sdf_m.format(new java.util.Date());
        java.util.Date date1;
        java.util.Date date0;
        try {
            date1 = sdf_m.parse(str);
            date0= sdf_m.parse(isMonth);
            if(date0.after(date1)){ 
                bea = true;
            }
        } catch (ParseException e) {
        	logger.error(e.toString());
            bea = false;
        }
        return bea;
    }
    
    /**
     * 比较当前日期和指定日期 return boolean
     * 如果当前日期在指定日期之后返回true否则返回flase
     * @param str
     * @return 
     * 2006-1-15 5:27:16 made in YeQiangWei
     */
    public static boolean secondCompare(String str){
        boolean bea = false;
        SimpleDateFormat sdf_d = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String isDate = sdf_d.format(new java.util.Date());
        java.util.Date date1;
        java.util.Date date0;
        try {
            date1 = sdf_d.parse(str);
            date0= sdf_d.parse(isDate);
            if(date0.after(date1)){ 
                bea = true;
            }
        } catch (ParseException e) {
        	logger.error(e.toString());
            bea = false;
        }
        return bea;
    }
    

    /**
     * 比较指定两日期如果str1晚于str2则return false; 精确到秒
     * @param str1
     * @param str2
     * @return
     * 2005-11-7 5:26:35 Made In YeQiangWei
     * com.yeqiangwei.tools
     */
    public static boolean secondCompare(String str1, String str2){
        boolean bea = false;
        SimpleDateFormat sdf_d = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        java.util.Date date1;
        java.util.Date date0;
        try {
            date1 = sdf_d.parse(str1);
            date0= sdf_d.parse(str2);
            if(date0.after(date1)){ 
                bea = true;
            }
        } catch (ParseException e) {
        	logger.error(e.toString());
            bea = false;
        }
        return bea;
    }
    
    
    
    /**
     * 比较指定两日期如果str1晚于str2则return flase; 精确到天
     * @param str1
     * @param str2
     * @return
     * 2005-11-7 5:26:35 Made In YeQiangWei
     * com.yeqiangwei.tools
     */
    public static boolean dateCompare(String str1, String str2){
        boolean bea = false;
        SimpleDateFormat sdf_d = new SimpleDateFormat("yyyy-MM-dd");
        java.util.Date date1;
        java.util.Date date0;
        try {
            date1 = sdf_d.parse(str1);
            date0= sdf_d.parse(str2);
            if(date0.after(date1)){ 
                bea = true;
            }
        } catch (ParseException e) {
        	logger.error(e.toString());
            bea = false;
        }
        return bea;
    }

    /**
     * 设置间隔数后返回时间
     * @param type
     * @param i
     * @return
     */
    public static String dateAdd(int type, int i){
        return dateAdd(type, i, "yyyy-MM-dd HH:mm:ss");  
    }
   


	/**
	 * 给指定的时间按间隔数设置后返回间隔时间
	 * @param str 格式化过的时间，要和fromat_str格式化方式一致
	 * @param type 间隔数类型 s秒 d天 h小时
	 * @param i 间隔数
	 * @param fromat_str 格式化时间的方式 如yyyy-MM-dd HH:mm:ss
	 * @return 
	 * 2006-1-15 5:23:48 made in YeQiangWei
	 */
    public static String dateAdd(String time, int type, int i, String fromat_str)
    {	
        SimpleDateFormat df = new SimpleDateFormat(fromat_str);
        Calendar c = Calendar.getInstance();   // 当时的日期和时间
        int s = c.get(type)+i;
        c.set(type, s);
        time = df.format(c.getTime());
        return time;        
    }
    
   

   /*
        String s1 =FormatDateTime.dateAdd("h",2,"yyyy-MM-dd HH:mm:ss");
        System.out.println(s1);
        String now = FormatDateTime.formatDateTime("yyyy-MM-dd HH:mm:ss");
    	System.out.println(now);
        System.out.print(FormatDateTime.secondCompare(s1,now));
    	System.out.print(FormatDateTime.dateCompare("2005-11-06","2005-10-06"));
    	
    	System.out.println(TypeChange.stringToInt(
    	FormatDateTime.dateAdd("s",-6000,"ddHHmmss")));
    	System.out.println(FormatDateTime.dateAdd("h",2,"yyyy-MM-dd HH:mm:ss"));
    	
    	//boolean bea = FormatDateTime.secondCompare(
				//FormatDateTime.dateAdd("2006-5-3 12:19:49","s",10,"yyyy-MM-dd HH:mm:ss")
				//,FormatDateTime.formatDateTime("yyyy-MM-dd HH:mm:ss"));
    	//System.out.println(bea);
    	//System.out.println(FormatDateTime.formatDateTime("yyyy-MM-dd HH:mm:ss"));
    	
    	System.out.print(
    			FormatDateTime.secondCompare(
    					"2006-3-3 12:19:49"
						,FormatDateTime.dateAdd("s",10,"yyyy-MM-dd HH:mm:ss"))
    	);
    }
    */
    /**
	 * 得到指定日期的间隔的日期 date 指定的日期为String
	 * date 指定的日期
	 * i    间隔的天数 v比较两个指定的日期相差多少天 返回int
	 * @throws ParseException
	 * xufeng
	 */
	public static String dayAdd(String date, int i)  {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar c = null;
		try {
			Date date2 = sdf.parse(date);
			c = Calendar.getInstance();
			c.setTime(date2);
			int day = i + c.get(Calendar.DAY_OF_MONTH);
			c.set(Calendar.DAY_OF_MONTH, day);
		} catch (ParseException e) {			
			e.printStackTrace();
		}
		
		return sdf.format(c.getTime());

	}


	/**
	 * 比较两个指定的日期相差多少天 返回int
	 * @throws ParseException
	 * xufeng
	 */
	 public static long getTowDaySpace(String date1,String date2){
		 SimpleDateFormat sdf_d = new SimpleDateFormat("yyyy-MM-dd");
			java.util.Date utildate1;
			java.util.Date utildate2;
			long total=0;
			try {
				utildate1 = sdf_d.parse(date1);
				utildate2 = sdf_d.parse(date2);
				long i=utildate2.getTime()-utildate1.getTime();
				total=i/(1000*60*60*24);
				
			} catch (ParseException e) {
				//logger.error(e.toString());
				
			}
			return total;
	 }


}
